from typing import List, Optional, Set, Dict, Tuple
import uvicorn
from pydantic import BaseModel
from fastapi import FastAPI

app = FastAPI()


# class Item(BaseModel):
#     name: str
#     description: Optional[str] = None
#     price: float
#     tax: Optional[float] = None
#     tags: List[str] = []
#     address: Set[str] = set()
#     phone: Tuple[int] = ()
#     ext: Dict[str, str] = {}


# 模型一
class Image(BaseModel):
    url: str
    name: str


# 模型二
class Item(BaseModel):
    name: str
    price: float
    description: Optional[str] = None
    tags: Set[str] = set()
    # Image 模型组成的列表类型
    image: Optional[List[Image]] = None


@app.post("/items/{item_id}")
async def update_item(
        item_id: int,
        # 声明类型为：嵌套模型的 Item
        item: Item):
    results = {"item_id": item_id, "item": item}
    return results

'''
期望的请求体
    {
      "name": "string",
      "price": 0,
      "description": "string",
      "tags": [
        "lxq", "lxq", "lzc"
      ],
      "image": [
        {
          "url": "string",
          "name": "string"
        }
      ]
    }
'''


'''
    FastAPI 给嵌套模型提供的功能
    和前面讲的没什么区别
    
    IDE 智能代码提示，甚至对于嵌套模型也支持
    数据转换
    数据验证
    OpenAPI 文档

'''

if __name__ == "__main__":
    uvicorn.run(app="main2:app", host="127.0.0.1", port=8000, reload=True, debug=True)